<?php

/**
 * ZFEngine
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://zfengine.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zfengine.com so we can send you a copy immediately.
 *
 * @category   ZFEngine
 * @package    ZFEngine_Module
 * @copyright  Copyright (c) 2009-2010 Stepan Tanasiychuk (http://stfalcon.com)
 * @license    http://zfengine.com/license/new-bsd     New BSD License
 * @version    $Id$
 */

/**
 *  Index Controller for module Pages
 *
 * @category   ZFEngine
 * @package    ZFEngine_Module
 * @subpackage Pages
 * @author     Stepan Tanasiychuk (http://stfalcon.com)
 * @license    http://zfengine.com/license/new-bsd     New BSD License
 *
 * @property   ZFEngine_Module_Pages_Form_Page_Delete $formDelete
 * @property   ZFEngine_Module_Pages_Form_Page_Edit $formEdit
 * @property   ZFEngine_Module_Pages_Form_Page_New $formNew
 */
abstract class ZFEngine_Module_Pages_Controller_Index extends Zend_Controller_Action
{

    /**
     * Get page service layer
     *
     * @return ZFEngine_Module_Pages_Model_PageService
     */
    protected function _getPageService()
    {
        return $this->_helper->serviceLayer->get('page', __METHOD__);
    }

    /**
     * New page
     *
     * @return void
     */
    public function newAction()
    {
        $this->view->setTitle(_('Новая страница'));
        $pageService = $this->_getPageService();
        if ($this->_request->isPost()) {
            if ($pageService->processNew($this->getRequest()->getPost())) {
                $this->_afterNewRedirect($pageService);
            }
        }
        $this->view->form = $pageService->formNew;
    }

    /**
     * Редирект после создания страницы
     * @param ZFEngine_Module_Pages_Model_PageService $service
     */
    protected function _afterNewRedirect($service)
    {
        $this->_helper->redirector->gotoRoute(array(
                'alias' => $service->getModel()->alias
        ), 'pages-view', true);
    }

    /**
     * View page
     *
     * @return void
     */
    public function viewAction()
    {
        if ($this->_getPageService()->findPageByAlias($this->_request->getParam('alias'))) {
            $this->view->page = $this->_getPageService()->getModel();

            $this->view->headTitle($this->_getPageService()->getModel()->title);
            $this->view->headMeta()->appendName('keywords', $this->_getPageService()->getModel()->keywords);
            $this->view->headMeta()->appendName('description', $this->_getPageService()->getModel()->description);
        } else {
            $this->_forward('not-found', 'error', 'default');
        }
    }

    /**
     * Edit page
     *
     * @return void
     */
    public function editAction()
    {
        $this->view->setTitle(_('Редактировать страницу'));

        $pageId = (int) $this->_request->getParam('id');
        
        if ($this->_getPageService()->findPageById($pageId)) {
            if ($this->_request->isPost()) {
                $postData = $this->_request->getPost();
                if ($this->_getPageService()->processFormEdit($postData)) {
                    $this->_afterEditRedirect($this->_getPageService());
                }
            } else {
                $this->_getPageService()->getModel()->Translation;
                $this->_getPageService()->formEdit->populate($this->_getPageService()->getModel()->toArray());
            }
            $this->view->form = $this->_getPageService()->formEdit;
        } else {
            $this->_forward('not-found', 'error', 'default');
        }

        $this->_getPageService()->getModel()->Translation;
        $this->_getPageService()->formEdit->populate($this->_getPageService()->getModel()->toArray());
    }

    /**
     * Редирект после редактирования страницы
     * @param ZFEngine_Module_Pages_Model_PageService $service
     */
    protected function _afterEditRedirect($service)
    {
        $this->_helper->redirector->gotoRoute(array(
                'alias' => $service->getModel()->alias
        ), 'pages-view', true);
    }

    /**
     * Delete page
     *
     * @return void
     */
    public function deleteAction()
    {
        $this->view->setTitle(_('Удалить старинцу'));

        if ($this->_getPageService()->findPageById((int) $this->_request->getParam('id'))) {
            $this->view->title = sprintf(
                $this->view->translate('Вы хотите удалить страницу "%s"?'),
                $this->_getPageService()->getModel()->title
            );
            $this->view->form = $this->_getPageService()->formDelete;

            if ($this->_request->isPost()) {
                $postData = $this->_request->getPost();
                $this->_getPageService()->processFormDelete($postData);
                $this->_afterDeleteRedirect($this->_getPageService());
            }
        } else {
            $this->_forward('not-found', 'error', 'default');
        }
    }

    /**
     * Редирект после удаления страницы
     * @param ZFEngine_Module_Pages_Model_PageService $service
     */
    protected function _afterDeleteRedirect($service)
    {
        $this->_helper->redirector->gotoRoute(array(
            'module' => 'pages',
            'controller' => 'index',
            'action' => 'list'
        ), 'default', true);
    }

    /**
     * View page
     *
     * @return void
     */
    public function listAction()
    {
        $this->view->setTitle(_('Страницы'));

        $query = $this->_getPageService()->getMapper()->findAllAsQuery();

        $this->view->paginator = $this->_helper->paginator->getPaginator($query);
    }
}